package com.antgroup.seckill.business.common.rocketmq;

import com.alibaba.fastjson.JSON;
import com.antgroup.seckill.business.common.vo.response.GoodsVo;
import com.antgroup.seckill.business.goods.model.SeckillMessage;
import com.antgroup.seckill.business.goods.model.SeckillOrder;
import com.antgroup.seckill.business.goods.service.IGoodsService;
import com.antgroup.seckill.business.goods.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author wangxy
 * @date 2024/8/11 17:29
 */
@Service
@Slf4j
@RocketMQMessageListener(topic = "seckill-order", consumerGroup = "goods-seckill-consumer-group")
public class SeckillRcketMqConsumer implements RocketMQListener<MessageExt> {

    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IOrderService iOrderService;

   /* @Override
    public void onMessage(String message) {

        log.info("接收到的消息为：" + message);

        SeckillMessage seckillMessage = JSON.parseObject(message, SeckillMessage.class);

        GoodsVo goodsVo = goodsService.queryGoodsByGoodsId(seckillMessage.getGoogsId());
        if(goodsVo.getSeckillCount() < 1) {
            return;
        }

        //判断是否重复抢购
        SeckillOrder seckillOrder = (SeckillOrder) redisTemplate.opsForValue().get("seckill:" + seckillMessage.getUser().getId() + ":" + seckillMessage.getGoogsId());
        if (Objects.nonNull(seckillOrder)) {
            return;
        }

        //下单
        iOrderService.seckillGoods(goodsVo,seckillMessage.getUser());

    }*/

   @Override
    public void onMessage(MessageExt messageExt) {
        log.info("seckill-order message: {}", new String(messageExt.getBody()));
        String topic = messageExt.getTopic();
        String tag = messageExt.getTags();
        String message = new String(messageExt.getBody());

        if(StringUtils.equals(topic,"seckill-order")) {
            if(StringUtils.equals(tag,"CREATE")) {
                //下单操作
                SeckillMessage seckillMessage = JSON.parseObject(message, SeckillMessage.class);
                GoodsVo goodsVo = goodsService.queryGoodsByGoodsId(seckillMessage.getGoogsId());
                if(goodsVo.getSeckillCount() < 1) {
                    return;
                }

                //判断是否重复抢购
                SeckillOrder seckillOrder = (SeckillOrder) redisTemplate.opsForValue().get("seckill:" + seckillMessage.getUser().getId() + ":" + seckillMessage.getGoogsId());
                if (Objects.nonNull(seckillOrder)) {
                    return;
                }

                //下单
                iOrderService.seckillGoods(goodsVo,seckillMessage.getUser());

            }
        }
    }

}
